package com.ljpz.manage.service;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljpz.common.bean.ItemCatData;
import com.ljpz.common.bean.ItemCatResult;
import com.ljpz.common.service.RedisService;
import com.ljpz.manage.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils  ;

/**
 * Created by liujunchen on 2016/11/27.
 *
 */
@Service
public class ItemCatService extends BaseService<ItemCat> {
    @Autowired
    private RedisService redisService ;
    private   static  final ObjectMapper  MAPPER  = new ObjectMapper()   ;
    private  static   final  String REDIS_KEY = "LJPZ_MANAGE_ITEM_CAT_API" ; //规则:项目名_模块名_业务名
    private  static  final  Integer REDIS_TIME=60*60*24*30*3;

    /*
    @Override
    public Mapper<ItemCat> getMapper() {
        return this.itemCatMapper  ;
    }*/
    /*    public List<ItemCat> queryItemCat(Long parentId ){
        ItemCat  record  =   new ItemCat()   ;
         record.setParentId(parentId);//查询参数
         return    this.itemCatMapper.select(record)  ;
    }*/
 public ItemCatResult queryAllToTree(){
     ItemCatResult   itemCatResult = new ItemCatResult() ;
     //先从缓存中命中， 如果命中就返回， 没有命中就继续执行

     try {

         String cacheData = this.redisService.get(REDIS_KEY);
         if (StringUtils.isNotEmpty(cacheData)) {
             //命中
             //反序列化
             return MAPPER.readValue(cacheData, ItemCatResult.class);
         }
     }catch (Exception e ){
         e.printStackTrace();
     }

     //查出对应的所有明细类目
     List<ItemCat> cats = super.queryAll()   ;
     //全部查出ItemCat所有类目
     //把itemCat 的数据都写对应的map中  按照  key 为父类id
     Map<Long , List<ItemCat>> itemCatMap = new HashMap<Long , List<ItemCat>>()  ;
     for(ItemCat  itemCat : cats)
     {
         //不存在容器中把itemcat添加进入容器
         if(!itemCatMap.containsKey(itemCat.getParentId())){
             itemCatMap.put(itemCat.getParentId(),new ArrayList<ItemCat>())  ;
         }
         itemCatMap.get(itemCat.getParentId()).add(itemCat);
     }
     List<ItemCat>  itemCats =  itemCatMap.get(0L)  ;
     List<ItemCatData>itemCatDataList  = new ArrayList<ItemCatData>();
     itemCatResult.setItemCats(itemCatDataList);
     //导入对应一级类目到itemCatResult
     for (ItemCat itemCat :itemCats ){
         ItemCatData   itemCatData =  new ItemCatData() ;
         itemCatData.setUrl("/products/"+itemCat.getId()+".html");
         itemCatData.setName("<a href='/products/"+itemCat.getId()+".html'>"+itemCat.getName()+"</a>");
         itemCatDataList.add(itemCatData);
         if(!itemCat.getParent()) {
             continue;
         }
         //导入对应二级类目到itemCatResult中去
         List<ItemCat>itemCats1 = itemCatMap.get(itemCat.getId());
         List<ItemCatData>itemCatDataList1   =   new ArrayList<ItemCatData>();
         itemCatData.setItems(itemCatDataList1);
         for(ItemCat itemCat1:itemCats1){
             ItemCatData  itemCatData1 = new ItemCatData()  ;
             itemCatData1.setUrl("/products/"+itemCat1.getId()+".html");
             itemCatData1.setName("<a href='/products/"+itemCat1.getId()+".html'>"+itemCat1.getName()+"</a>");
             //添加到二级目录中
             itemCatDataList1.add(itemCatData1)  ;
             if(itemCat1.getParent()) {
                 //得到三级目录对应的记录
                 List<ItemCat> itemCats2 = itemCatMap.get(itemCat1.getId());
                 List<String> itemCatDataList2 = new ArrayList<String>();
                 itemCatData1.setItems(itemCatDataList2);
                 //封装三级目录到对象中去
                 for (ItemCat itemCat2 : itemCats2) {
                     itemCatDataList2.add("/products/" + itemCat2.getId() + ".html|" + itemCat2.getName());
                 }
             }

         }
         if(itemCatResult.getItemCats().size()>=14){
             break;
         }

     }
     try {
         //将数据库查询结果写入到结果集中 , 序列化为json字符串
         this.redisService.set(REDIS_KEY,REDIS_TIME,MAPPER.writeValueAsString(itemCatResult));
     } catch (Exception e) {
         e.printStackTrace();
     }

     return itemCatResult;

 }
}
